<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> 
<head>
<title>CLisp Compiler Sample</title>
<link rel="stylesheet" type="text/css" href="../../../docs/rotor.css">
</head>

<body>		


<h1>CLisp Compiler Sample</h1>


<h2>Sample Overview</h2>


<p>CLisp is a sample Lisp compiler, which compiles to common intermediate 
language (CIL). CLisp is developed 
in C# and uses reflection emit to generate CIL. It can also be adapted to 
function as an 
interpreter if the generated CIL is executed dynamically instead of persisting 
to an assembly file.<br>
&nbsp;<br>
It compiles functions like <b>car</b>, <b>cdr</b>, <b>cons</b>, <b>atom</b>, <b>null</b>, 
<b>subst</b>. Not all Lisp 
functions are implemented, but others can be added with ease, as they are 
implemented as a runtime library. Control flow structures such as if-then-else 
and do-while have been implemented. Recursive function calls are supported.</p>


<p>The compiler consists of the following modules in separate source files.</p>


<p><b>Lexer</b></p>


<blockquote>


<p>Converts the input file into a token stream. Strips out the source code 
comments.</p>


</blockquote>


<p><b>Parser</b></p>


<blockquote>


<p>Parses the token stream outputting an abstract syntax tree (AST). Syntax errors are reported, but 
error recovery is not robust. Multiple errors are not reported.</p>


</blockquote>


<p><b>Semantic Analyzer</b></p>


<blockquote>


<p>Analyzes the AST and augments it with semantic information using a bottom-up 
approach. Currently <b>Int</b> and <b>lists</b> are the only types. Floating point 
numbers are not 
supported.</p>


</blockquote>


<p><b>Code Generator</b></p>


<blockquote>


<p>Visits the AST and generates corresponding CIL.</p>


</blockquote>


<h2>Sample Source and Build Output Locations</h2>


<p>The sample source is found in sscli20\samples\compilers\clisp.&nbsp; The source 
files are:</p>


<ul class="none">
  <li><a href="clisp.cs">clisp.cs</a></li>
  <li><a href="codegen.cs">codegen.cs</a></li>
  <li><a href="exp.cs">exp.cs</a></li>
  <li><a href="lexer.cs">lexer.cs</a></li>
  <li><a href="lexer.cs">lispruntime.cs</a></li>
  <li><a href="parser.cs">parser.cs</a></li>
  <li><a href="token.cs">token.cs</a></li>
  <li><a href="typecheck.cs">typecheck.cs</a></li>
</ul>


<p>The build output location is %_NTTREE%\samples\compilers\clisp.&nbsp; 
The output file is an executable assembly named clisp.exe.</p>


<h2>Building the Sample</h2>


<p>All samples are built by the buildall script.&nbsp;  </p>


<p>You can also build all the 
samples by switching to the root of the sample directory, sscli20\samples, and typing 
<code>build -c</code>.</p>


<p>You can build this specific sample  by switching to the sample directory and typing 
<code>build -c</code>.</p>


<h2>Running the Sample</h2>


<ol>
  <li>Run env.bat </li>
    <li>Build the SSCLI using the buildall script or batch file. </li>
  <li>Switch to the %_NTTREE%\samples\compilers\clisp directory.</li>
  <li>Type the following command:<blockquote>


<p>clix clisp <i>inputfile</i></p>


  </blockquote>


<p>where <i>inputfile</i> is the name of a Lisp source file.&nbsp; For example:</p>


  <blockquote>


<p>clix clisp fibo.lisp</p>


  </blockquote>
  </li>
  <li>Run the resulting assembly:</li>
</ol>
<blockquote>
  <blockquote>


<p>clix fibo.exe</p>


  </blockquote>
</blockquote>


<hr>


<p><i>Copyright (c) 2006 Microsoft Corporation. All rights reserved.</i></p>
</body> 
</html>